// 集合
using System;
namespace CollectionSapce
{
    class CollectionCalss
    {
        static void Main(string[] args)
        {
            // ArrayList 集合也叫动态数组 详情查看Array.cs
            // 集合 - Hashtable
            Hashtable ht = new Hashtable();
            ht.Add("001", "Zara Ali");
            ht.Add("002", "Abida Rehman");
            ht.Add("003", "Joe Holzner");
            ht.Add("004", "Mausam Benazir Nur");
            ht.Add("005", "M. Amlan");
            ht.Add("006", "M. Arif");
            ht.Add("007", "Ritesh Saikia");
            if (ht.ContainsValue("Nuha Ali"))
            {
                Console.WriteLine("This student name is already in the list");
            }
            else
            {
                ht.Add("008", "Nuha Ali");
            } 
            ICollection key = ht.Keys; // 获取键的集合

            foreach (string k in key)
            {
                Console.WriteLine(k + ": " + ht[k]);
            }
            IDictionaryEnumerator enumeratorht = ht.GetEnumerator();
            while (enumeratorht.MoveNext())
            {
                Console.WriteLine((string)enumeratorht.Key + "---" + (string)enumeratorht.Value + "\t");
            }
            // 集合 - SortedList
            SortedList sl = new SortedList(); 
            sl.Add("001", "Zara Ali");
            sl.Add("002", "Abida Rehman");
            sl.Add("003", "Joe Holzner");
            sl.Add("004", "Mausam Benazir Nur");
            sl.Add("005", "M. Amlan");
            sl.Add("006", "M. Arif");
            sl.Add("007", "Ritesh Saikia"); 
            if (sl.ContainsValue("Nuha Ali"))
            {
                Console.WriteLine("This student name is already in the list");
            }
            else
            {
                sl.Add("008", "Nuha Ali");
            } 
            ICollection SortedListKey = sl.Keys; // 获取一系列按照键来排序的键的集合 
            foreach (string k in SortedListKey)
            {
                Console.WriteLine(k + ": " + sl[k]);
            }
            IEnumerator enumerator = sl.GetEnumerator();
            while (enumerator.MoveNext())
            {
                DictionaryEntry entry = (DictionaryEntry)enumerator.Current;
                Console.WriteLine("IEnumerator:"+(string)entry.Key + "---" + (string)entry.Value + "\t");
            }
            // 集合 - Stack
            Stack st = new Stack();

            st.Push('A');
            st.Push('M');
            st.Push('G');
            st.Push('W');

            Console.WriteLine("Current stack: ");
            foreach (char c in st)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine();

            st.Push('V');
            st.Push('H');
            Console.WriteLine("The next poppable value in stack: {0}", st.Peek());
            Console.WriteLine("Current stack: ");
            foreach (char c in st)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine();

            Console.WriteLine("Removing values ");
            st.Pop();
            st.Pop();
            st.Pop();

            Console.WriteLine("Current stack: ");
            foreach (char c in st)
            {
                Console.Write(c + " ");
            }
            // 集合 - Queue 
            Queue q = new Queue();

            q.Enqueue('A');
            q.Enqueue('M');
            q.Enqueue('G');
            q.Enqueue('W');

            Console.WriteLine("Current queue: ");
            foreach (char c in q) { 
                Console.Write(c + " ");
            }
            Console.WriteLine();
            q.Enqueue('V');
            q.Enqueue('H');
            Console.WriteLine("Current queue: ");
            foreach (char c in q)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine();
            Console.WriteLine("Removing some values ");
            char ch = (char)q.Dequeue();
            Console.WriteLine("The removed value: {0}", ch);
            ch = (char)q.Dequeue();
            Console.WriteLine("The removed value: {0}", ch);
            Console.WriteLine("Current queue2: ");
            foreach (char c in q)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine();
            IEnumerator enumeratorq = q.GetEnumerator(); // IEnumerator
            while (enumeratorq.MoveNext())
            {
                Console.WriteLine("IEnumerator:{0}",enumeratorq.Current); // 输出每个元素的值
            }
            // 创建两个大小为 8 的点阵列
            BitArray ba1 = new BitArray(8);
            BitArray ba2 = new BitArray(8);
            byte[] a = { 60 };
            byte[] b = { 13 };

            // 把值 60 和 13 存储到点阵列中
            ba1 = new BitArray(a);
            ba2 = new BitArray(b);

            // ba1 的内容
            Console.WriteLine("Bit array ba1: 60");
            for (int i = 0; i < ba1.Count; i++)
            {
                Console.Write("{0, -6} ", ba1[i]);
            }
            Console.WriteLine();

            // ba2 的内容
            Console.WriteLine("Bit array ba2: 13");
            for (int i = 0; i < ba2.Count; i++)
            {
                Console.Write("{0, -6} ", ba2[i]);
            }
            Console.WriteLine();


            BitArray ba3 = new BitArray(8);
            ba3 = ba1.And(ba2); // ba1改变了
            // ba1 的内容
            Console.WriteLine("Bit array ba1: 60");
            for (int i = 0; i < ba1.Count; i++)
            {
                Console.Write("{0, -6} ", ba1[i]);
            }
            Console.WriteLine();
            // ba3 的内容
            Console.WriteLine("Bit array ba3 after AND operation: 12");
            for (int i = 0; i < ba3.Count; i++)
            {
                Console.Write("{0, -6} ", ba3[i]);
            }
            Console.WriteLine();

            ba3 = ba1.Or(ba2);  // ba1改变了
            // ba1 的内容
            Console.WriteLine("Bit array ba1: 60");
            for (int i = 0; i < ba1.Count; i++)
            {
                Console.Write("{0, -6} ", ba1[i]);
            }
            Console.WriteLine();
            // ba3 的内容
            Console.WriteLine("Bit array ba3 after OR operation: 61");
            for (int i = 0; i < ba3.Count; i++)
            {
                Console.Write("{0, -6} ", ba3[i]);
            }
            Console.WriteLine();


            // 创建自定义集合的实例
            CustomCollection myCollection = new CustomCollection();

            // 向集合中添加元素
            myCollection.Add("Apple");
            myCollection.Add("Banana");
            myCollection.Add("Cherry");

            // 遍历集合并输出元素
            Console.WriteLine("自定义集合:");
            foreach (object item in myCollection)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            // 向集合中删除元素
            myCollection.Remove("Cherry");
            Console.WriteLine("自定义集合:");
            foreach (object item in myCollection)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine(myCollection.Count);
            Console.WriteLine(myCollection[0]);

            // 泛型集合 - List
            List<int> intList = new List<int>();

            // 泛型集合 - Dictionary 
            Dictionary<string, int> dictionary = new Dictionary<string, int>();// 创建一个空的字典
            // 向字典中添加元素
            dictionary.Add("apple", 1);
            dictionary.Add("banana", 2);
            dictionary.Add("cherry", 3);

            // 访问字典中的元素
            int appleCount = dictionary["apple"];
            Console.WriteLine("苹果的数量是：" + appleCount);

            // 检查字典中是否包含某个键
            if (dictionary.ContainsKey("banana"))
            {
                Console.WriteLine("字典中包含香蕉。");
            }

            // 遍历字典中的键值对
            foreach (KeyValuePair<string, int> kvp in dictionary)
            {
                Console.WriteLine($"键: {kvp.Key}, 值: {kvp.Value}");
            }

            // 移除字典中的元素
            dictionary.Remove("banana");

            // 尝试获取一个键对应的值，如果键不存在则返回默认值
            int orangeCount = dictionary.TryGetValue("orange", out int orangeValue) ? orangeValue : 0;
            Console.WriteLine("橙子的数量是：" + orangeCount);

            // 清空字典
            dictionary.Clear();
        }
    }
    // 自定义集合类
    public class CustomCollection : CollectionBase
    {
        // 添加元素到集合中
        public int Add(object item)
        {
            return List.Add(item);
        }

        // 移除元素
        public void Remove(object item)
        {
            List.Remove(item);
        }

        // 索引器，允许通过索引访问集合中的元素
        public object this[int index]
        {
            get { return List[index]; }
            set { List[index] = value; }
        }

        // 获取集合中的元素数量
        public int Count
        {
            get { return List.Count; }
        }

        // 枚举器，允许遍历集合中的元素
        public IEnumerator GetEnumerator()
        {
            return List.GetEnumerator();
        }
        
    }
}